package p2pscrabble;

import static javax.swing.TransferHandler.MOVE;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class LetterTile extends JLabel {
	private char letter;
	private ImageIcon letterImage;
	private boolean blankTile;
	private boolean onBoard;
	private boolean fixed;
	private JTextField blankLetter;

	public LetterTile(char letter, boolean isOnBoard, boolean blank,
			boolean isFixed) {
		this.letter = letter;
		this.onBoard = isOnBoard;
		this.blankTile = blank;
		this.fixed = isFixed;

		this.setFont(new java.awt.Font("System", Font.BOLD, 18));
		this.setTransferHandler(new MyTransferHandler());

		if (!fixed) {
			addMouseListener(new LetterTileMouseAdapter());
			addMouseMotionListener(new LetterTileMouseAdapter());
		}

		if (!blank) {
			String imageFile = ".\\img\\tile_" + letter
					+ ".jpg";
			letterImage = new ImageIcon(imageFile);
			this.setIcon(letterImage);
		} else {
			this.setBorder(javax.swing.BorderFactory.createEtchedBorder(null,
					Color.BLUE));
			String text = "" + letter;
			this.setText(text);
		}
	}

	public char getLetter() {
		return letter;
	}

	public void setLetter(char letter) {
		this.letter = letter;
	}

	public boolean isBlank() {
		return blankTile;
	}

	public boolean isOnBoard() {
		return onBoard;
	}

	public void setOnBoard(boolean onBoard) {
		this.onBoard = onBoard;
	}

	public boolean isFixed() {
		return fixed;
	}

	public void paint(Graphics g) {
		this.setSize(27, 27);
		super.paint(g);
	}

	class LetterTileMouseAdapter extends MouseAdapter {
		public LetterTileMouseAdapter() {
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			// System.out.println(e.getSource());
			if (blankTile) {
				blankLetter = new JTextField(1);

				KeyAdapter blank = new KeyAdapter() {

					@Override
					public void keyTyped(KeyEvent e) {
						if (blankLetter.getText().length() > 0)
							blankLetter.setText(blankLetter.getText()
									.substring(0, 0));
						else
							return;
					}

					@Override
					public void keyPressed(KeyEvent e) {
						char lt;
						if (e.getKeyCode() == KeyEvent.VK_ENTER) {
							if (!blankLetter.getText().equals("")) {
								if (blankLetter.getText().charAt(0) <= 90)
									lt = (char) (blankLetter.getText().charAt(0) + 32);
								else
									lt = blankLetter.getText().charAt(0);
								LetterTile.this.setLetter(lt);
								LetterTile.this.setText(blankLetter.getText());
							}
							LetterTile.this.remove(blankLetter);
						}
					}

				};

				LetterTile.this.add(blankLetter);
				blankLetter.setBounds(0, 0, 27, 27);

				blankLetter.addKeyListener(blank);
			}
		}

		@Override
		public void mouseEntered(MouseEvent e) {
		}

		@Override
		public void mouseExited(MouseEvent e) {
		}

		@Override
		public void mousePressed(MouseEvent e) {

		}

		@Override
		public void mouseReleased(MouseEvent e) {
		}

		@Override
		public void mouseDragged(MouseEvent e) {
			getTransferHandler().exportAsDrag(LetterTile.this, e, MOVE);
			repaint();
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			repaint();
		}
	}
}
